package com.cangchu.ui.activity;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.cangchu.MyApplication;
import com.cangchu.util.Injector;

import java.lang.reflect.ParameterizedType;

/**
 * 基础活动类，实现一些可能用得到的效果。这里继承自AppCompatActivity。保留菜单栏。
 * Created by Administrator on 2016/10/26.
 */
public abstract class BaseActivity<T> extends AppCompatActivity {


    private Class<T> cls;

    /**
     * 返回日志Tag，方便配置，减少代码
     *
     * @return
     */
    protected String getLogTag() {

        if (cls == null) {
            cls = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        }
        return cls.getSimpleName();

    }


    protected Context context = null;
    //网络连接状态的管理类[暂时用不到]
    protected ConnectivityManager connectivityManager;
    private MyApplication Application;
    private ActionBar actionBar;

    //进度条
    public ProgressDialog Dialog;
    public AlertDialog AlertDialog;

    @Override
    //重写onCreate生命周期，初始化一些需要用到的属性
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //使用代码将屏幕设置为竖直状态，感觉暂时用不到。
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        context = this;
        Application = (MyApplication) getApplication();

        if (getContentViewLayoutID() != 0) {
            setContentView(getContentViewLayoutID());
        } else {
            throw new IllegalArgumentException(
                    "You must return a right contentView layout resource Id");
        }

        Dialog = new ProgressDialog(context);
        Dialog.setCancelable(false);


        initViewsAndEvents();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        Injector.get(this).inject();
    }

    public MyApplication getmApplication() {
        return Application;
    }


    public void closeAlertDialog() {
        if (this.AlertDialog != null) {
            this.AlertDialog.dismiss();
            this.AlertDialog = null;
        }
    }


    //提供了一个不需要回调函数的接口……
    public void callErrMsg(String msg) {
        showAlertDialog("提示", msg, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
    }

    public void showAlertDialog(String title, String msg, String posbtn,
                                DialogInterface.OnClickListener listener) {
        closeAlertDialog();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        AlertDialog = builder.setTitle(title).setMessage(msg)
                .setPositiveButton(posbtn, listener).create();
        AlertDialog.show();
    }

    //带取消键的对话框
    public void showAlertDialog(String title, String msg, String posbtn,
                                DialogInterface.OnClickListener listener, String negbtn,
                                DialogInterface.OnClickListener neglistener) {
        closeAlertDialog();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        AlertDialog = builder.setTitle(title).setMessage(msg)
                .setPositiveButton(posbtn, listener)
                .setNegativeButton(negbtn, neglistener).create();
        AlertDialog.show();
    }


    protected abstract int getContentViewLayoutID();

    protected abstract void initViewsAndEvents();


    @Nullable
    @Override
    public ActionBar getActionBar() {
        return actionBar;
    }

    public void setActionBar(ActionBar actionBar) {
        this.actionBar = actionBar;
    }
}
